package com.wangjw.springbootdemo.zkdemo;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by wangjw on 18/5/30.
 */
public class ZKConnect implements Watcher {

    private static final Logger log = LoggerFactory.getLogger(ZKConnect.class);

    public static final String zkServerPath = "118.126.91.173:2181";
    public static final Integer timeout = 5000; //毫秒

    public static void main(String[] args) throws Exception {
        ZooKeeper zooKeeper = new ZooKeeper(zkServerPath, timeout, new ZKConnect());

        log.warn("客户端开始连接zookeeper服务器...");
        log.warn("连接状态：{}", zooKeeper.getState());

        new Thread().sleep(2000);

        log.warn("连接状态：{}", zooKeeper.getState());
    }

    @Override
    public void process(WatchedEvent event) {
        log.warn("接收到watch通知：{}", event);
    }
}
